package net.zjitc.servlet;

import net.zjitc.domain.Product;
import net.zjitc.service.ProductService;
import net.zjitc.service.impl.ProductServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/2/28 10:43
 */
@WebServlet(name = "FileServlet",urlPatterns = "/fileAdd")
public class FileServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Product product = new Product();
        ProductService service = new ProductServiceImpl();
        //创建工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //创建文件解析工具
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            for (FileItem item :fileItems){
                if(item.isFormField()){
                    if(item.getFieldName().equals("pname")) {
                        String value = item.getString();
                        product.setPname(value);
                    }
                    if(item.getFieldName().equals("marketprice")) {
                        String value = item.getString();
                        Double values = Double.parseDouble(value);
                        product.setMarketprice(values);
                    }
                    if(item.getFieldName().equals("shopprice")) {
                        product.setShopprice(Double.parseDouble(item.getString()));
                    }
                    if(item.getFieldName().equals("ishot")) {
                        product.setIshot(Integer.parseInt(item.getString()));
                    }
                    if(item.getFieldName().equals("pdesc")) {
                        product.setPdesc(item.getString());
                    }
                    else {
                        String fileName = item.getName();
                        System.out.println("fileName");
                        String path = request.getServletContext().getRealPath("/products/5");
                        File file  = new File(path);

                        //当此文件夹不存在的时候，创建此文件夹
                        if (!file.exists()){
                            file.mkdirs();
                        }
                        //创建文件
                        File f = new File(path,fileName);
                        if (!f.exists()){
                            f.createNewFile();
                        }
                        //读取的文件流写入此文件
                        item.write(f);

                        //设置产品的图片属性，也就是数据库中要存入的路径
                        product.setImage("products/5/"+fileName);
                    }
                }
            }
            service.add(product);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("/findDetail").forward(request,response);

    }
}
